<template>
  <div class="check-progress-container">
    <h2>盘点进度</h2>
    
    <el-card class="filter-card">
      <el-form :inline="true" :model="searchParams" class="search-form">
        <el-form-item label="盘点计划">
          <el-select v-model="searchParams.planId" placeholder="选择盘点计划">
            <el-option label="2024年Q1季度盘点" value="1" />
            <el-option label="2024年2月月度盘点" value="2" />
            <el-option label="IT设备专项盘点" value="3" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="状态">
          <el-select v-model="searchParams.status" placeholder="选择状态">
            <el-option label="全部" value="" />
            <el-option label="未开始" value="notStarted" />
            <el-option label="进行中" value="inProgress" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="searchProgress">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <el-card class="overview-card">
      <template #header>
        <div class="card-header">
          <span>进度概览</span>
          <el-button type="text" @click="refreshOverview">
            <el-icon><Refresh /></el-icon> 刷新
          </el-button>
        </div>
      </template>
      
      <div class="progress-stats">
        <div class="stat-item">
          <div class="stat-number">{{ totalAssets }}</div>
          <div class="stat-label">总资产数</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ checkedAssets }}</div>
          <div class="stat-label">已盘点</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ remainingAssets }}</div>
          <div class="stat-label">待盘点</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ diffAssets }}</div>
          <div class="stat-label">发现差异</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ completionRate }}%</div>
          <div class="stat-label">完成率</div>
        </div>
      </div>
      
      <div class="progress-bar">
        <el-progress :percentage="completionRate" :status="getProgressStatus()" />
      </div>
    </el-card>
    
    <el-card class="departments-card">
      <template #header>
        <div class="card-header">
          <span>部门盘点进度</span>
        </div>
      </template>
      
      <div class="departments-progress">
        <div
          v-for="dept in departmentsProgress"
          :key="dept.id"
          class="dept-progress-item"
        >
          <div class="dept-info">
            <div class="dept-name">{{ dept.name }}</div>
            <div class="dept-stats">
              <span>{{ dept.checked }}/{{ dept.total }}</span>
              <span class="dept-rate">{{ dept.rate }}%</span>
            </div>
          </div>
          <el-progress
            :percentage="dept.rate"
            :status="getDeptProgressStatus(dept.rate)"
            :show-text="false"
            class="dept-progress-bar"
          />
        </div>
      </div>
    </el-card>
    
    <el-card class="recent-records-card">
      <template #header>
        <div class="card-header">
          <span>最近盘点记录</span>
        </div>
      </template>
      
      <el-table :data="recentRecords" style="width: 100%">
        <el-table-column prop="assetCode" label="资产编号" width="180" />
        <el-table-column prop="assetName" label="资产名称" />
        <el-table-column prop="assetType" label="资产类型" width="120" />
        <el-table-column prop="department" label="所属部门" width="120" />
        <el-table-column prop="checkBy" label="盘点人" width="100" />
        <el-table-column prop="checkTime" label="盘点时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.checkTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag
              :type="getStatusTagType(scope.row.status)"
              :effect="getStatusTagEffect(scope.row.status)"
            >
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" fixed="right">
          <template #default="scope">
            <el-button type="text" size="small" @click="viewRecordDetail(scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="recordTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <el-dialog
      v-model="dialogVisible"
      title="盘点记录详情"
      width="50%"
      :before-close="handleClose"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="资产编号">{{ selectedRecord.assetCode }}</el-descriptions-item>
        <el-descriptions-item label="资产名称">{{ selectedRecord.assetName }}</el-descriptions-item>
        <el-descriptions-item label="资产类型">{{ selectedRecord.assetType }}</el-descriptions-item>
        <el-descriptions-item label="资产原值">{{ selectedRecord.originalValue }}元</el-descriptions-item>
        <el-descriptions-item label="当前状态">{{ selectedRecord.currentStatus }}</el-descriptions-item>
        <el-descriptions-item label="所属部门">{{ selectedRecord.department }}</el-descriptions-item>
        <el-descriptions-item label="使用人">{{ selectedRecord.user }}</el-descriptions-item>
        <el-descriptions-item label="存放位置">{{ selectedRecord.location }}</el-descriptions-item>
        <el-descriptions-item label="盘点人">{{ selectedRecord.checkBy }}</el-descriptions-item>
        <el-descriptions-item label="盘点时间">{{ formatDate(selectedRecord.checkTime) }}</el-descriptions-item>
        <el-descriptions-item label="盘点结果">{{ getStatusText(selectedRecord.status) }}</el-descriptions-item>
        <el-descriptions-item label="备注" v-if="selectedRecord.remark">{{ selectedRecord.remark }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { Refresh } from '@element-plus/icons-vue'

export default {
  name: 'CheckProgress',
  components: {
    Refresh
  },
  data() {
    return {
      searchParams: {
        planId: '',
        status: ''
      },
      totalAssets: 1250,
      checkedAssets: 876,
      remainingAssets: 374,
      diffAssets: 23,
      completionRate: 69.9,
      departmentsProgress: [
        { id: 1, name: '技术部', total: 350, checked: 320, rate: 91.4 },
        { id: 2, name: '市场部', total: 280, checked: 230, rate: 82.1 },
        { id: 3, name: '行政部', total: 150, checked: 120, rate: 80.0 },
        { id: 4, name: '财务部', total: 120, checked: 96, rate: 80.0 },
        { id: 5, name: '人力资源部', total: 90, checked: 70, rate: 77.8 },
        { id: 6, name: '运营部', total: 260, checked: 40, rate: 15.4 }
      ],
      recentRecords: [
        {
          id: 1,
          assetCode: 'IT-2023-0001',
          assetName: 'Dell笔记本电脑',
          assetType: '电子设备',
          department: '技术部',
          checkBy: '张三',
          checkTime: new Date('2024-03-15 10:23:45'),
          status: 'normal',
          originalValue: 6800,
          currentStatus: '在用',
          user: '张三',
          location: 'A栋5楼技术部'
        },
        {
          id: 2,
          assetCode: 'OF-2023-0045',
          assetName: '办公椅',
          assetType: '办公家具',
          department: '市场部',
          checkBy: '李四',
          checkTime: new Date('2024-03-15 10:15:30'),
          status: 'normal',
          originalValue: 800,
          currentStatus: '在用',
          user: '王五',
          location: 'B栋3楼市场部'
        },
        {
          id: 3,
          assetCode: 'IT-2022-0132',
          assetName: 'HP打印机',
          assetType: '电子设备',
          department: '行政部',
          checkBy: '赵六',
          checkTime: new Date('2024-03-15 09:45:12'),
          status: 'lost',
          originalValue: 1200,
          currentStatus: '丢失',
          user: '钱七',
          location: 'A栋2楼行政部',
          remark: '盘点未找到设备，疑似丢失'
        }
      ],
      recordTotal: 150,
      currentPage: 1,
      pageSize: 10,
      dialogVisible: false,
      selectedRecord: {}
    }
  },
  methods: {
    searchProgress() {
      console.log('搜索盘点进度:', this.searchParams)
      // 这里应该调用后端API获取数据
    },
    resetSearch() {
      this.searchParams = {
        planId: '',
        status: ''
      }
    },
    refreshOverview() {
      console.log('刷新概览数据')
      // 这里应该重新获取数据
    },
    getProgressStatus() {
      if (this.completionRate >= 100) return 'success'
      if (this.completionRate >= 70) return 'primary'
      if (this.completionRate >= 30) return 'warning'
      return 'danger'
    },
    getDeptProgressStatus(rate) {
      if (rate >= 100) return 'success'
      if (rate >= 70) return 'primary'
      if (rate >= 30) return 'warning'
      return 'danger'
    },
    getStatusTagType(status) {
      const typeMap = {
        normal: 'success',
        lost: 'danger',
        damage: 'warning',
        moved: 'info'
      }
      return typeMap[status] || 'info'
    },
    getStatusTagEffect(status) {
      return status === 'lost' || status === 'damage' ? 'dark' : 'light'
    },
    getStatusText(status) {
      const textMap = {
        normal: '正常',
        lost: '丢失',
        damage: '损坏',
        moved: '位置不符'
      }
      return textMap[status] || '未知'
    },
    formatDate(date) {
      if (!date) return ''
      const d = new Date(date)
      return d.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      })
    },
    viewRecordDetail(record) {
      this.selectedRecord = { ...record }
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
      this.selectedRecord = {}
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.loadRecords()
    },
    handleCurrentChange(current) {
      this.currentPage = current
      this.loadRecords()
    },
    loadRecords() {
      console.log('加载记录:', { page: this.currentPage, size: this.pageSize })
      // 这里应该调用后端API获取分页数据
    }
  }
}
</script>

<style scoped>
.check-progress-container {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}

.search-form {
  margin: 0;
}

.overview-card,
.departments-card,
.recent-records-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
}

.progress-stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 30px;
  padding: 20px 0;
}

.stat-item {
  text-align: center;
  padding: 10px;
  border-radius: 8px;
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  min-width: 120px;
}

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #3b82f6;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #64748b;
}

.progress-bar {
  margin: 20px;
}

.departments-progress {
  padding: 10px 0;
}

.dept-progress-item {
  margin-bottom: 15px;
}

.dept-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
}

.dept-name {
  font-weight: 500;
}

.dept-stats {
  display: flex;
  align-items: center;
  gap: 10px;
}

.dept-rate {
  font-weight: bold;
  color: #3b82f6;
}

.dept-progress-bar {
  height: 8px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>